package cn.jd.pet.controller;

import cn.jd.basic.util.LoginContext;
import cn.jd.org.domain.Employee;
import cn.jd.org.service.EmployeeService;
import cn.jd.pet.dto.AcceptDto;
import cn.jd.pet.service.ISearchMasterMsgService;
import cn.jd.pet.domain.SearchMasterMsg;
import cn.jd.pet.query.SearchMasterMsgQuery;
import cn.jd.basic.util.AjaxResult;
import cn.jd.basic.util.PageList;
import cn.jd.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private EmployeeService employeeService;


    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.remove(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryByPage(query);
    }

    /**
     * 寻主数据添加
     * @param
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request)
    {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee=employeeService.findByEmpLoginId(logininfo.getId());
        searchMasterMsg.setUserId(employee.getId());
        searchMasterMsgService.publish(searchMasterMsg);
        return AjaxResult.me().success() ;
    }
    /**
     * 获取已完成数据
     * @param
     * @return
     */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request)
    {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee=employeeService.findByEmpLoginId(logininfo.getId());
        // 判断是否是店铺管理员还是平台管理员
        if(employee.getShopId()!=null){
            query.setShopId(employee.getShopId());
        }
        query.setState(3);
        return  searchMasterMsgService.toauditTohandlePollFinish(query);
    }
    /**
     * 获取寻主池的数据
     * @param
     * @return
     */
    @PostMapping("/toSearchMasterPool")
    public PageList<SearchMasterMsg> toSearchMasterPool(@RequestBody SearchMasterMsgQuery query)
    {
        query.setState(1);
        return  searchMasterMsgService.toauditTohandlePollFinish(query);
    }
    /**
     * 待处理的数据
     * @param
     * @return
     */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request)
    {
        query.setState(0);
        return  searchMasterMsgService.toauditTohandlePollFinish(query);
    }

    /**
     * 待处理数据
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request)
    {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee=employeeService.findByEmpLoginId(logininfo.getId());
        query.setShopId(employee.getShopId());
        query.setState(1);
        return  searchMasterMsgService.toauditTohandlePollFinish(query);
    }
    /**
     * accept
     * 接单
     * @param
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptDto dto, HttpServletRequest request)
    {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee=employeeService.findByEmpLoginId(logininfo.getId());
        searchMasterMsgService.accept(dto,employee);
        return AjaxResult.me().success();
    }
    /**
     * let url = "/searchMasterMsg/reject/" + row.id;
     * 拒单
     */
    @PostMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable("id") Long id, HttpServletRequest request)
    {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee=employeeService.findByEmpLoginId(logininfo.getId());
         searchMasterMsgService.reject(id,employee);
        return AjaxResult.me().success();
    }

}
